package com.stars.easyms.alarm.autoconfigure;

import com.stars.easyms.alarm.AlarmInterceptor;
import com.stars.easyms.alarm.EasyMsAlarmService;
import com.stars.easyms.alarm.client.EasyMsAlarmClient;
import com.stars.easyms.alarm.client.dingtalk.EasyMsDingTalkAlarmClient;
import com.stars.easyms.alarm.client.email.EasyMsEmailAlarmClient;
import com.stars.easyms.alarm.client.sms.EasyMsSmsAlarmClient;
import com.stars.easyms.alarm.client.wechat.EasyMsWeChatAlarmClient;
import com.stars.easyms.alarm.properties.*;
import com.stars.easyms.alarm.service.EasyMsAsynAlarmService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>className: EasyMsAlarmAutoConfiguration</p>
 * <p>description: 告警自动配置类</p>
 *
 * @author caotieshuan
 * @version 1.5.0
 * @date 2019-12-19 16:22
 */
@Configuration
@EnableConfigurationProperties(EasyMsAlarmProperties.class)
public class EasyMsAlarmAutoConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(EasyMsAlarmAutoConfiguration.class);

    private final EasyMsAlarmProperties alarmProperties;

    private final List<EasyMsAlarmClient> alarmClientList = new ArrayList<>();

    private final AlarmInterceptor alarmInterceptor;

    public EasyMsAlarmAutoConfiguration(EasyMsAlarmProperties alarmProperties, ObjectProvider<AlarmInterceptor> alarmInterceptors) {
        this.alarmProperties = alarmProperties;
        this.alarmInterceptor = alarmInterceptors.getIfAvailable();
    }

    @Bean
    public EasyMsAsynAlarmService easyMsAsynAlarmService() {
        return new EasyMsAsynAlarmService(alarmClientList);
    }

    @Bean
    public EasyMsAlarmService easyMsAlarmService(EasyMsAsynAlarmService easyMsAsynAlarmService) {
        return new EasyMsAlarmService(alarmProperties, alarmClientList, easyMsAsynAlarmService, alarmInterceptor);
    }

    @PostConstruct
    private void init() {
        if (alarmProperties.isEnabled()) {

            // SMS客户端创建及初始化
            EasyMsSmsAlarmClient smsAlarmClient = new EasyMsSmsAlarmClient(alarmProperties);
            if (smsAlarmClient.initAndReturnIsValid()) {
                alarmClientList.add(smsAlarmClient);
            }

            // EMAIL客户端创建及初始化
            EasyMsEmailAlarmClient emailAlarmClient = new EasyMsEmailAlarmClient(alarmProperties);
            if (emailAlarmClient.initAndReturnIsValid()) {
                alarmClientList.add(emailAlarmClient);
            }

            // 钉钉客户端创建及初始化
            EasyMsDingTalkAlarmClient dingTalkAlarmClient = new EasyMsDingTalkAlarmClient(alarmProperties);
            if (dingTalkAlarmClient.initAndReturnIsValid()) {
                alarmClientList.add(dingTalkAlarmClient);
            }

            // 企业微信客户端创建及初始化
            EasyMsWeChatAlarmClient weChatAlarmClient = new EasyMsWeChatAlarmClient(alarmProperties);
            if (weChatAlarmClient.initAndReturnIsValid()) {
                alarmClientList.add(weChatAlarmClient);
            }
        }

        logger.info("Easy-ms alarm module is{} enabled!", alarmClientList.isEmpty() ? " not" : "");

    }

}